package com.scau.jansing.thread.first.resource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by jansing on 2015/12/2.
 */
public class EvenChecker implements Runnable {
    private IntGenerator generator;
    private int id;

    public EvenChecker(int id, IntGenerator generator){
        this.id = id;
        this.generator = generator;
    }

    @Override
    public void run() {
        while(!generator.isCanceled()){
            int val = generator.next();
            if(val % 2 != 0){
                System.out.println(val + " not even!");
                generator.cancel();
            }
        }
    }



    public static void test(IntGenerator generator, int count){
        System.out.println("Press Control-C to exit");
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i = 0; i<count; i++){
            exec.execute(new EvenChecker(i, generator));
        }
        exec.shutdown();
    }

    public static void test(IntGenerator generator){
        test(generator, 10);
    }
}
